//
//  CaseDetail.m
//  Home
//
//  Created by 陶亮 on 16/7/13.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "CaseDetail.h"
#import "WUEditViewController.h"
@implementation CaseDetail
{
    
    
    NSMutableArray * limgArray;
    NSMutableArray * simgArray;
     NSArray *array;
    NSMutableArray *_photos;
    NSMutableArray *_thumbs;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{


    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addUI];
    }
    
    return self;

}

-(void)addUI{



    
        self.BackimageView = [UIImageView new];
        self.BackimageView.userInteractionEnabled=YES;
        self.BackimageView.layer.masksToBounds = YES;
        self.BackimageView.layer.cornerRadius = 5 * AUTO_WIDTH;
        [self addSubview:self.BackimageView];
        self.BackimageView.backgroundColor = [UIColor whiteColor];
        [self.BackimageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0  * AUTO_HEIGHT);
            make.left.mas_equalTo(16/2 * AUTO_WIDTH);
            make.right.mas_equalTo(self.mas_right).offset(- 8 * AUTO_WIDTH);
            make.height.mas_equalTo(KScreenHeight * 3 / 2 * AUTO_HEIGHT);
    
        }];
 
        _myImageView = [UIImageView new];
        [self.BackimageView addSubview:_myImageView];
        _myImageView.userInteractionEnabled=YES;
        [_myImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.BackimageView.mas_top).offset(7 * AUTO_HEIGHT);
            make.left.mas_equalTo(self.BackimageView.mas_left).offset(7 * AUTO_WIDTH);
            make.right.mas_equalTo(self.BackimageView.mas_right).offset(- 7 * AUTO_WIDTH);
            make.height.mas_equalTo(210 * AUTO_HEIGHT);
    
        }];
    
     array=@[@"u=957005295,3708602591&fm=206&gp=0.jpg",@"u=957005295,3708602591&fm=206&gp=0.jpg",@"u=957005295,3708602591&fm=206&gp=0.jpg"];
    
        self.imgView=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-32 *AUTO_WIDTH, 210 *AUTO_HEIGHT) imageURLStringsGroup:array];

        self.imgView.clipsToBounds=YES;
        self.imgView.delegate=self;
        self.imgView.userInteractionEnabled=YES;
        self.imgView.delegate=self;
        self.imgView.infiniteLoop=YES;
        self.imgView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
        self.imgView.autoScrollTimeInterval = 3;
        [_myImageView addSubview:self.imgView];
    
    
    
        _nameL = [UILabel new];
        _nameL.text = @"湖滨花园";
        _nameL.textColor = GH;
        _nameL.font = [UIFont fontWithName:TFont size:TEXTNAMEFONT * AUTO_WIDTH];
        [self.BackimageView addSubview:_nameL];
        [_nameL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_myImageView.mas_bottom).offset(5 * AUTO_HEIGHT);
            make.centerXWithinMargins .mas_equalTo(self.BackimageView);
            make.height.mas_equalTo(70/2 * AUTO_HEIGHT);
    
        }];
    
        UIImageView *line = [UIImageView new];
        [self.BackimageView addSubview:line];
        line.image = [UIImage imageNamed:@"虚线"];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_nameL.mas_bottom).offset(5 * AUTO_HEIGHT);
            make.left.mas_equalTo((14  )* AUTO_WIDTH);
            make.right.mas_equalTo(- (14) * AUTO_WIDTH);
            make.height.mas_equalTo(1.5 * AUTO_HEIGHT);
    
        }];
    
        //钱
        UIImageView *priceImageView = [UIImageView new];
        priceImageView.image = [UIImage imageNamed:@"qiang2x"];
        [self.BackimageView addSubview:priceImageView];
        [priceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line.mas_bottom).offset(20 * AUTO_HEIGHT);
            make.left.mas_equalTo(36 * AUTO_WIDTH);
            make.size.mas_equalTo(CGSizeMake(15 * AUTO_WIDTH, 15 * AUTO_WIDTH));
    
        }];
    
        UIImageView *suxian1 = [UIImageView new];
        [self.BackimageView addSubview:suxian1];
        suxian1.image = [UIImage imageNamed:@"虚线竖"];
        [suxian1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line.mas_bottom).offset(10 * AUTO_HEIGHT);
            make.left.mas_equalTo(85 * AUTO_WIDTH);
            make.height.mas_equalTo(60 * AUTO_HEIGHT);
        }];
    
        _priceL = [UILabel new];
        _priceL.text = @"30";
        _priceL.textColor = ZI;
        _priceL.textAlignment = NSTextAlignmentCenter;
        _priceL.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
        [self.BackimageView addSubview:_priceL];
        [_priceL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(priceImageView.mas_bottom).offset(10 * AUTO_HEIGHT);
            make.height.mas_equalTo(priceImageView);
            make.left.mas_equalTo(self.BackimageView);
            make.right.mas_equalTo(suxian1.mas_right);
    
    
        }];
    
    
        //定位
        UIImageView *PingFangImageView = [UIImageView new];
        PingFangImageView.image=[UIImage imageNamed:@"mi"];
        [self.BackimageView addSubview:PingFangImageView];
        [PingFangImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line.mas_bottom).offset(20 * AUTO_HEIGHT);
            make.left.mas_equalTo(suxian1.mas_right).offset(35 * AUTO_WIDTH);
            make.size.mas_equalTo(CGSizeMake(30/2 * AUTO_WIDTH, 30/2 * AUTO_WIDTH));
    
        }];
    
    
        UIImageView *suxian2 = [UIImageView new];
        [self.BackimageView addSubview:suxian2];
        suxian2.image = [UIImage imageNamed:@"虚线竖"];
        [suxian2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line.mas_bottom).offset(10 * AUTO_HEIGHT);
            make.left.mas_equalTo(85 *2* AUTO_WIDTH);
            make.height.mas_equalTo(60 * AUTO_HEIGHT);
        }];
        _sizeL = [UILabel new];
        _sizeL = [UILabel new];
        _sizeL.text = @"110";
        _sizeL.textColor = ZI;
        _sizeL.textAlignment = NSTextAlignmentCenter;
        _sizeL.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
        [self.BackimageView addSubview:_sizeL];
        [_sizeL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(PingFangImageView.mas_bottom).offset(10 * AUTO_HEIGHT);
            make.height.mas_equalTo(priceImageView);
            make.left.mas_equalTo(90 *AUTO_WIDTH);
            make.right.mas_equalTo(suxian2.mas_right);
    
    
        }];
    
        //建筑设计
        UIImageView *homeIV = [UIImageView new];
        [self.BackimageView addSubview:homeIV];
        homeIV.image = [UIImage imageNamed:@"fangwu"];
        [homeIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line.mas_bottom).offset(18 * AUTO_HEIGHT);
            make.left.mas_equalTo(suxian2.mas_right).offset(33 * AUTO_WIDTH);
            make.size.mas_equalTo(CGSizeMake(18 * AUTO_WIDTH, 18 * AUTO_WIDTH));
    
        }];
    
    
    
        UIImageView *suxian3 = [UIImageView new];
        [self.BackimageView addSubview:suxian3];
        suxian3.image = [UIImage imageNamed:@"虚线竖"];
        [suxian3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line.mas_bottom).offset(10 * AUTO_HEIGHT);
            make.left.mas_equalTo(85 * 3* AUTO_WIDTH);
            make.height.mas_equalTo(60 * AUTO_HEIGHT);
        }];
    
    
    
        _styleL = [UILabel new];
        _styleL = [UILabel new];
        _styleL.text = @"平层户型";
        _styleL.textColor = ZI;
        _styleL.textAlignment = NSTextAlignmentCenter;
        _styleL.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
        [self.BackimageView addSubview:_styleL];
        [_styleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(PingFangImageView.mas_bottom).offset(10 * AUTO_HEIGHT);
            make.height.mas_equalTo(priceImageView);
            make.left.mas_equalTo(suxian2);
            make.right.mas_equalTo(suxian3.mas_right);
    
    
        }];
    
        //风格
        UIImageView *addressIV = [UIImageView new];
        [self.BackimageView addSubview:addressIV];
        addressIV.image = [UIImage imageNamed:@"fenge"];
        [addressIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line.mas_bottom).offset(20 * AUTO_HEIGHT);
            make.left.mas_equalTo(suxian3.mas_right).offset(36 * AUTO_WIDTH);
            make.size.mas_equalTo(CGSizeMake(15 * AUTO_WIDTH, 15 * AUTO_WIDTH));
    
    
        }];
    
        _addressL = [UILabel new];
        _addressL = [UILabel new];
        _addressL = [UILabel new];
        _addressL.text = @"地中海";
        _addressL.textColor = ZI;
        _addressL.textAlignment = NSTextAlignmentCenter;
        _addressL.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
        
        [self.BackimageView addSubview:_addressL];
        [_addressL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(PingFangImageView.mas_bottom).offset(10 * AUTO_HEIGHT);
            make.height.mas_equalTo(priceImageView);
            make.left.equalTo(suxian3.mas_right).offset(24*AUTO_WIDTH);
            
        }];
        
        UIImageView *lineBottom = [UIImageView new];
        [self.BackimageView addSubview:lineBottom];
        lineBottom.image = [UIImage imageNamed:@"虚线"];
        [lineBottom mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(suxian1.mas_bottom).offset(10 * AUTO_HEIGHT);
            make.left.width.height.mas_equalTo(line);
            
        }];



}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"tap tag is %ld", index);
    // Browser
    _photos = [[NSMutableArray alloc] init];
    _thumbs = [[NSMutableArray alloc] init];
    for (int i=0; i<limgArray.count; i++)
    {
//        MWPhoto *photo = [MWPhoto photoWithImage:[UIImage imageNamed:array[i]]];
        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:limgArray[i]]];
        [_photos addObject:photo];
    }
    
    //    //调用父View的里面的方法
    //    WUDetailViewController *vc = (WUDetailViewController *)self.superview.superview.nextResponder;
    //    NSLog(@"%@", self.superview.superview.nextResponder);
    WUEditViewController * vc = (WUEditViewController *)self.superview.superview.superview.nextResponder;
    
    // Create browser
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    browser.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    browser.displayActionButton = NO;//分享按钮,默认是
    browser.displayNavArrows = YES;//左右分页切换,默认否
    browser.displaySelectionButtons = NO;//是否显示选择按钮在图片上,默认否
    browser.alwaysShowControls = NO;//控制条件控件 是否显示,默认否
    browser.zoomPhotosToFill = YES;//是否全屏,默认是
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
    browser.wantsFullScreenLayout = YES;//是否全屏
#endif
    browser.enableGrid = YES;//是否允许用网格查看所有图片,默认是
    browser.startOnGrid = NO;//是否第一张,默认否
    browser.enableSwipeToDismiss = YES;
    [browser showNextPhotoAnimated:YES];
    [browser showPreviousPhotoAnimated:YES];
    [browser setCurrentPhotoIndex:index];
    
    
    [vc.navigationController pushViewController:browser animated:NO];
}

#pragma mark - MWPhotoBrowserDelegate
-(id<MWPhoto>) photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index
{
    return _photos[index];
}

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}


- (id )photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}

-(void)settingModel:(ResignModel *)model{
    
    
    _nameL.text = model.title;
    
     _priceL.text = model.money;
    
    _sizeL.text = model.mianji;
    _styleL.text = model.huxing;

      _addressL.text = model.style;
    
    
    
    
    limgArray = [NSMutableArray new];
    simgArray = [NSMutableArray new];
    for (NSMutableDictionary *dic  in model.imgs) {
        [limgArray insertObject: [NSString stringWithFormat:@"%@%@",HTTPURL,[dic objectForKey:@"limg"]] atIndex:limgArray.count];
        [simgArray insertObject:[NSString stringWithFormat:@"%@%@",HTTPURL,[dic objectForKey:@"simg"]] atIndex:simgArray.count];
    }
    self.imgView.imageURLStringsGroup = simgArray;
    array = simgArray;


}

- (void)Back:(UIButton *)btu{
     WUEditViewController * vc = (WUEditViewController *)self.superview.superview.superview.nextResponder;
    [vc.navigationController popViewControllerAnimated:YES];
}









@end
